package com.we.wecc.aw.mid.impl;

import com.google.common.collect.Lists;
import com.we.wecc.aw.db.mapper.AwardMapper;
import com.we.wecc.aw.db.mapper.FriendsBoostMapper;
import com.we.wecc.aw.db.model.Award;
import com.we.wecc.aw.db.model.FriendsBoost;
import com.we.wecc.aw.db.model.FriendsBoostExample;
import com.we.wecc.aw.mid.model.out.FriendsBoostOut;
import com.we.wecc.aw.mid.service.FriendsBoostService;
import com.we.wecc.us.db.mapper.UserMapper;
import com.we.wecc.us.db.model.User;
import com.x.share.mid.exception.HelperException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class FriendsBoostServiceImpl implements FriendsBoostService {

    @Resource
    FriendsBoostMapper friendsBoostMapper;

    @Resource
    UserMapper userMapper;

    @Resource
    AwardMapper awardMapper;

    @Override
    public FriendsBoostOut getFriendsList(Long userId, int awardId) {
        FriendsBoostExample ex = new FriendsBoostExample();
        ex.or().andUserIdEqualTo(userId).andAwardIdEqualTo(awardId);
        List<FriendsBoost> list = friendsBoostMapper.selectByExample(ex);
        List<FriendsBoostOut.UserInfo> userInfos = Lists.newArrayList();
        list.forEach(s -> {
            User us = userMapper.selectByPrimaryKey(s.getFriendsId());
            userInfos.add(FriendsBoostOut.UserInfo.from(us));
        });
        return FriendsBoostOut.from(userId, awardId, userInfos);
    }

    @Override
    public FriendsBoost boost(Long awardUserId, int awardId, Long userId) {
        Award award = awardMapper.selectByPrimaryKey(awardId);
        Long now = System.currentTimeMillis();
        if (awardUserId.equals(userId)) {
            throw new HelperException("自己不能给自己助力哦！");
        }
        FriendsBoostExample ex = new FriendsBoostExample();
        ex.or().andUserIdEqualTo(awardUserId).andFriendsIdEqualTo(userId).andAwardIdEqualTo(awardId);
        List<FriendsBoost> list = friendsBoostMapper.selectByExample(ex);
        ex.clear();
        ex.or().andAwardIdEqualTo(awardId).andFriendsIdEqualTo(userId);
        int count = (int) friendsBoostMapper.countByExample(ex);
        if (count >= award.getBoostNum()) {
            throw new HelperException("您今天的助力次数已用完！");
        }
        if (!list.isEmpty()) {
            throw new HelperException("你已经给他助力过了，不能重复助力哦！");
        }
        if (now - award.getEndTime().getTime() >= 0) {
            throw new HelperException("该活动已过期！");
        }
        FriendsBoost fb = new FriendsBoost();
        fb.setAwardId(awardId);
        fb.setUserId(awardUserId);
        fb.setFriendsId(userId);
        fb.setCreateTime(new Date());
        friendsBoostMapper.insert(fb);
        return fb;
    }

    @Override
    public Integer getBoostNum(Long userId, int awardId) {
        FriendsBoostExample ex = new FriendsBoostExample();
        ex.or().andUserIdEqualTo(userId).andAwardIdEqualTo(awardId);
        int count = (int) friendsBoostMapper.countByExample(ex);
        return count;
    }
}
